package com.mpcm.web.client.core;

import com.gwtplatform.mvp.client.HasUiHandlers;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ContentSlot;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.proxy.Proxy;
import com.gwtplatform.mvp.client.proxy.RevealContentHandler;
import com.google.inject.Inject;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.gwt.user.client.Window;
import com.gwtplatform.mvp.client.proxy.RevealRootLayoutContentEvent;
import com.mpcm.web.client.page.home.HomePresenter;

public class HomeLayoutPresenter extends
		Presenter<HomeLayoutPresenter.MyView, HomeLayoutPresenter.MyProxy> implements HomeLayoutUiHandlers {

	public interface MyView extends View, HasUiHandlers<HomeLayoutUiHandlers> {
		void resetScrollPanel();
		void resetHeaderPosition();
	}

	@ProxyCodeSplit
	public interface MyProxy extends Proxy<HomeLayoutPresenter> {
	}

	@ContentSlot
    public static final Type<RevealContentHandler<?>> TYPE_RevealMainContent1 = new Type<RevealContentHandler<?>>();
	@ContentSlot
    public static final Type<RevealContentHandler<?>> TYPE_RevealMainContent2 = new Type<RevealContentHandler<?>>();
	@ContentSlot
    public static final Type<RevealContentHandler<?>> TYPE_RevealMainContent3 = new Type<RevealContentHandler<?>>();
	@ContentSlot
    public static final Type<RevealContentHandler<?>> TYPE_RevealMainContent4 = new Type<RevealContentHandler<?>>();
	@ContentSlot
    public static final Type<RevealContentHandler<?>> TYPE_RevealMainContent5 = new Type<RevealContentHandler<?>>();

	private int clientWidth = 0;
	
	private final HomePresenter homePresenter;
	
	@Inject
	public HomeLayoutPresenter(final EventBus eventBus, final MyView view,
			final MyProxy proxy, final HomePresenter homePresenter) {
		super(eventBus, view, proxy);
		getView().setUiHandlers(this);
		
		this.homePresenter = homePresenter;
	}

	@Override
	protected void revealInParent() {
		RevealRootLayoutContentEvent.fire(this, this);
	}

	@Override
	protected void onBind() {
		super.onBind();
	}

	@Override
	protected void onHide() {
		super.onHide();
	}

	@Override
	protected void onReset() {
		super.onReset();
		
		getView().resetHeaderPosition();
		setObjectsPosition();
	}

	@Override
	protected void onReveal() {
		super.onReveal();
		
		onSetClientWidth();
		setInSlot(TYPE_RevealMainContent1, homePresenter);
	}

	@Override
	protected void onUnbind() {
		super.onUnbind();
	}

	@Override
	public void onBaseScroll() {
		setObjectsPosition();
	}
	
	@Override
	public void onSetClientWidth() {
		clientWidth = Window.getClientWidth();
		
		if(homePresenter != null) {
			homePresenter.getView().setContainerWidth(clientWidth);
		}
	}
	
	private void setObjectsPosition() {
		if(homePresenter != null) {
			homePresenter.getView().setObjectsPosition(clientWidth);
		}
	}
}
